package mcheli;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import mcheli.aircraft.*;
import mcheli.command.MCH_GuiTitle;
import mcheli.gltd.MCH_ClientGLTDTickHandler;
import mcheli.gltd.MCH_EntityGLTD;
import mcheli.gltd.MCH_GuiGLTD;
import mcheli.gui.MCH_Gui;
import mcheli.helicopter.MCH_ClientHeliTickHandler;
import mcheli.helicopter.MCH_EntityHeli;
import mcheli.helicopter.MCH_GuiHeli;
import mcheli.lweapon.MCH_ClientLightWeaponTickHandler;
import mcheli.lweapon.MCH_GuiLightWeapon;
import mcheli.multiplay.MCH_GuiScoreboard;
import mcheli.multiplay.MCH_GuiTargetMarker;
import mcheli.multiplay.MCH_MultiplayClient;
import mcheli.plane.MCP_ClientPlaneTickHandler;
import mcheli.plane.MCP_EntityPlane;
import mcheli.plane.MCP_GuiPlane;
import mcheli.tank.MCH_ClientTankTickHandler;
import mcheli.tank.MCH_EntityTank;
import mcheli.tank.MCH_GuiTank;
import mcheli.tool.MCH_ClientToolTickHandler;
import mcheli.tool.MCH_GuiWrench;
import mcheli.tool.MCH_ItemWrench;
import mcheli.tool.rangefinder.MCH_GuiRangeFinder;
import mcheli.uav.MCH_EntityUavStation;
import mcheli.vehicle.MCH_ClientVehicleTickHandler;
import mcheli.vehicle.MCH_EntityVehicle;
import mcheli.vehicle.MCH_GuiVehicle;
import mcheli.weapon.MCH_GPSPosition;
import mcheli.weapon.MCH_RenderLaser;
import mcheli.weapon.MCH_WeaponSet;
import mcheli.wrapper.*;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.gui.GuiChat;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Vec3;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.common.MinecraftForge;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import java.util.ArrayList;
import java.util.List;

@SideOnly(Side.CLIENT)
public class MCH_ClientCommonTickHandler extends W_TickHandler {

    public static final float hitTotalDamageScaleOrigin = 2.0f;
    private static final ResourceLocation cross3rd = new ResourceLocation(W_MOD.DOMAIN, "textures/3rdCross.png");
    public static MCH_ClientCommonTickHandler instance;
    public static int cameraMode = 0;
    public static MCH_EntityAircraft ridingAircraft = null;
    public static boolean isDrawScoreboard = false;
    public static int sendLDCount = 0;
    public static boolean isLocked = false;
    public static int lockedSoundCount = 0;
    public static int hitDisplayCountdown;
    public static float hitTotalDamage;
    public static int hitTotalDamageClearCountdown;
    public static float hitTotalDamageScale = 2.0f;
    public static List<HitMessage> hitList = new ArrayList<>();
    public static boolean enableNew3rdCamera = true;
    public static boolean showVehicleCrossHair = false;
    @SideOnly(Side.CLIENT)
    public static EntityLivingBase camera;
    private static double prevMouseDeltaX;
    private static double prevMouseDeltaY;
    private static double mouseDeltaX = 0.0D;
    private static double mouseDeltaY = 0.0D;
    private static double mouseRollDeltaX = 0.0D;
    private static double mouseRollDeltaY = 0.0D;
    private static boolean isRideAircraft = false;
    private static float prevTick = 0.0F;
    public MCH_GuiCommon gui_Common;
    public MCH_Gui gui_Heli;
    public MCH_Gui gui_Plane;
    public MCH_Gui gui_Tank;
    public MCH_Gui gui_GLTD;
    public MCH_Gui gui_Vehicle;
    public MCH_Gui gui_LWeapon;
    public MCH_Gui gui_Wrench;
    public MCH_Gui gui_EMarker;
    public MCH_Gui gui_RngFndr;
    public MCH_Gui gui_Title;
    public MCH_Gui[] guis;
    public MCH_Gui[] guiTicks;
    public MCH_ClientTickHandlerBase[] ticks;
    public MCH_Key[] Keys;
    public MCH_Key KeyCamDistUp;
    public MCH_Key KeyCamDistDown;
    public MCH_Key KeyScoreboard;
    public MCH_Key KeyMultiplayManager;

    public MCH_ClientCommonTickHandler(Minecraft minecraft, MCH_Config config) {
        super(minecraft);
        this.gui_Common = new MCH_GuiCommon(minecraft);
        this.gui_Heli = new MCH_GuiHeli(minecraft);
        this.gui_Plane = new MCP_GuiPlane(minecraft);
        this.gui_Tank = new MCH_GuiTank(minecraft);
        this.gui_GLTD = new MCH_GuiGLTD(minecraft);
        this.gui_Vehicle = new MCH_GuiVehicle(minecraft);
        this.gui_LWeapon = new MCH_GuiLightWeapon(minecraft);
        this.gui_Wrench = new MCH_GuiWrench(minecraft);
        this.gui_RngFndr = new MCH_GuiRangeFinder(minecraft);
        this.gui_EMarker = new MCH_GuiTargetMarker(minecraft);
        this.gui_Title = new MCH_GuiTitle(minecraft);
        this.guis = new MCH_Gui[]{this.gui_RngFndr, this.gui_LWeapon, this.gui_Heli, this.gui_Plane, this.gui_Tank, this.gui_GLTD, this.gui_Vehicle};
        this.guiTicks = new MCH_Gui[]{this.gui_Common, this.gui_Heli, this.gui_Plane, this.gui_Tank, this.gui_GLTD, this.gui_Vehicle, this.gui_LWeapon, this.gui_Wrench, this.gui_RngFndr, this.gui_EMarker, this.gui_Title};
        this.ticks = new MCH_ClientTickHandlerBase[]{new MCH_ClientHeliTickHandler(minecraft, config), new MCP_ClientPlaneTickHandler(minecraft, config), new MCH_ClientTankTickHandler(minecraft, config), new MCH_ClientGLTDTickHandler(minecraft, config), new MCH_ClientVehicleTickHandler(minecraft, config), new MCH_ClientLightWeaponTickHandler(minecraft, config), new MCH_ClientSeatTickHandler(minecraft, config), new MCH_ClientToolTickHandler(minecraft, config)};
        this.updatekeybind(config);
        FMLCommonHandler.instance().bus().register(this);
        MinecraftForge.EVENT_BUS.register(this);
    }

    public static double getCurrentStickX() {
        return mouseRollDeltaX;
    }

    public static double getCurrentStickY() {
        double inv = 1.0D;
        if (Minecraft.getMinecraft().gameSettings.invertMouse) {
            inv = -inv;
        }

        if (MCH_Config.InvertMouse.prmBool) {
            inv = -inv;
        }

        return mouseRollDeltaY * inv;
    }

    public static double getMaxStickLength() {
        return 40.0D;
    }

    public static void addHitMessage(HitMessage message) {
        int maxMessage = 5;
        if (hitList.size() >= maxMessage) {
            hitList.remove(0);
        }
        hitList.add(message);
    }

    public static void addTotalDamage(float damage) {
        if (damage < 5) {
        } else if (damage < 12) {
            hitTotalDamageScale = 2.2f;
        } else if (damage < 35) {
            hitTotalDamageScale = 2.4f;
        } else if (damage < 60) {
            hitTotalDamageScale = 2.6f;
        } else {
            hitTotalDamageScale = 2.8f;
        }
        hitTotalDamage += damage;
    }

    public void updatekeybind(MCH_Config config) {
        this.KeyCamDistUp = new MCH_Key(MCH_Config.KeyCameraDistUp.prmInt);
        this.KeyCamDistDown = new MCH_Key(MCH_Config.KeyCameraDistDown.prmInt);
        this.KeyScoreboard = new MCH_Key(MCH_Config.KeyScoreboard.prmInt);
        this.KeyMultiplayManager = new MCH_Key(MCH_Config.KeyMultiplayManager.prmInt);
        this.Keys = new MCH_Key[]{this.KeyCamDistUp, this.KeyCamDistDown, this.KeyScoreboard, this.KeyMultiplayManager};
        MCH_ClientTickHandlerBase[] arr$ = this.ticks;

        for (MCH_ClientTickHandlerBase t : arr$) {
            t.updateKeybind(config);
        }

    }

    public void onTick() {
        Minecraft minecraft = Minecraft.getMinecraft();
        MCH_ClientTickHandlerBase.initRotLimit();
        MCH_Key[] player = this.Keys;
        int inOtherGui = player.length;

        for (int ac = 0; ac < inOtherGui; ++ac) {
            MCH_Key len$ = player[ac];
            len$.update();
        }

        EntityClientPlayerMP var7 = super.mc.thePlayer;
        if (var7 != null && super.mc.currentScreen == null) {
            if (MCH_ServerSettings.enableCamDistChange && (this.KeyCamDistUp.isKeyDown() || this.KeyCamDistDown.isKeyDown())) {
                inOtherGui = (int) W_Reflection.getThirdPersonDistance();
                if (this.KeyCamDistUp.isKeyDown() && inOtherGui < 60) {
                    inOtherGui += 4;
                    if (inOtherGui > 60) {
                        inOtherGui = 60;
                    }

                    W_Reflection.setThirdPersonDistance((float) inOtherGui);
                } else if (this.KeyCamDistDown.isKeyDown()) {
                    inOtherGui -= 4;
                    if (inOtherGui < 4) {
                        inOtherGui = 4;
                    }

                    W_Reflection.setThirdPersonDistance((float) inOtherGui);
                }
            }

            if (super.mc.currentScreen == null) {
                label85:
                {
                    if (super.mc.isSingleplayer()) {
                        if (!MCH_Config.DebugLog) {
                            break label85;
                        }
                    }

                    isDrawScoreboard = this.KeyScoreboard.isKeyPress();
                    if (!isDrawScoreboard && this.KeyMultiplayManager.isKeyDown()) {
                        MCH_PacketIndOpenScreen.send(5);
                    }
                }
            }
        }

        if (sendLDCount < 10) {
            ++sendLDCount;
        } else {
            MCH_MultiplayClient.sendImageData();
            sendLDCount = 0;
        }

        boolean var12 = super.mc.currentScreen != null;
        MCH_ClientTickHandlerBase[] var8 = this.ticks;
        int var10 = var8.length;

        int i$;
        for (i$ = 0; i$ < var10; ++i$) {
            MCH_ClientTickHandlerBase g = var8[i$];
            g.onTick(var12);
        }

        MCH_Gui[] var9 = this.guiTicks;
        var10 = var9.length;

        for (i$ = 0; i$ < var10; ++i$) {
            MCH_Gui var13 = var9[i$];
            var13.onTick();
        }

        MCH_EntityAircraft var11 = MCH_EntityAircraft.getAircraft_RiddenOrControl(var7);
        if (var7 != null && var11 != null && !var11.isDestroyed()) {
            if (isLocked && lockedSoundCount == 0) {
                isLocked = false;
                lockedSoundCount = 20;
                MCH_ClientTickHandlerBase.playSound("locked");
            }
        } else {
            lockedSoundCount = 0;
            isLocked = false;
        }

        if (lockedSoundCount > 0) {
            --lockedSoundCount;
        }

        if (hitDisplayCountdown > 0) {
            hitDisplayCountdown--;
            if (hitDisplayCountdown == 0) {
                hitList.clear();
            }
        }

        if (hitTotalDamageClearCountdown > 0) {
            hitTotalDamageClearCountdown--;
            if (hitTotalDamageClearCountdown == 0) {
                hitTotalDamage = 0;
            }
        }
        if (hitTotalDamageScale > hitTotalDamageScaleOrigin) {
            hitTotalDamageScale *= 0.95f;
            hitTotalDamageScale = Math.max(hitTotalDamageScale, hitTotalDamageScaleOrigin);
        }

        if (var7 != null && var7.ridingEntity == null) {
            MCH_Camera.currentCameraMode = 0;
        }

        //第三人称摄像机视角
        if (enableNew3rdCamera
            && minecraft.thePlayer.ridingEntity instanceof MCH_EntityAircraft
            && minecraft.gameSettings.thirdPersonView != 0) {
            if (camera == null) {
                camera = new MCH_3rdCamera(minecraft.theWorld, (MCH_EntityAircraft) minecraft.thePlayer.ridingEntity);
                minecraft.thePlayer.worldObj.spawnEntityInWorld(camera);
            }
            minecraft.renderViewEntity = camera;
            showVehicleCrossHair = true;
        } else {
            if (camera != null) {
                camera.setDead();
                camera = null;
            }
            showVehicleCrossHair = false;
        }

        //GPS
        if (minecraft.thePlayer.ridingEntity == null) {
            MCH_GPSPosition.currentClientGPSPosition.isActive = false;
        }

        MCH_RenderLaser.tickBeams();
    }

    public void onTickPre() {
        if (super.mc.thePlayer != null && super.mc.theWorld != null) {
            this.onTick();
        }

    }

    public void onTickPost() {
        if (super.mc.thePlayer != null && super.mc.theWorld != null) {
            MCH_GuiTargetMarker.onClientTick();
        }
        MCH_PlayerViewHandler.onUpdate();
    }

    public void updateMouseDelta(boolean stickMode, float partialTicks) {
        prevMouseDeltaX = mouseDeltaX;
        prevMouseDeltaY = mouseDeltaY;
        mouseDeltaX = 0.0D;
        mouseDeltaY = 0.0D;
        if (super.mc.inGameHasFocus && Display.isActive() && super.mc.currentScreen == null) {
            if (stickMode) {
                if (Math.abs(mouseRollDeltaX) < getMaxStickLength() * 0.2D) {
                    mouseRollDeltaX *= 1.0F - 0.15F * partialTicks;
                }

                if (Math.abs(mouseRollDeltaY) < getMaxStickLength() * 0.2D) {
                    mouseRollDeltaY *= 1.0F - 0.15F * partialTicks;
                }
            }

            super.mc.mouseHelper.mouseXYChange();
            float f1 = super.mc.gameSettings.mouseSensitivity * 0.6F + 0.2F;
            float f2 = f1 * f1 * f1 * 8.0F;
            double ms = MCH_Config.MouseSensitivity.prmDouble * 0.1D;
            mouseDeltaX = ms * (double) super.mc.mouseHelper.deltaX * (double) f2;
            mouseDeltaY = ms * (double) super.mc.mouseHelper.deltaY * (double) f2;
            byte inv = 1;
            if (super.mc.gameSettings.invertMouse) {
                inv = -1;
            }

            if (MCH_Config.InvertMouse.prmBool) {
                inv *= -1;
            }

            mouseRollDeltaX += mouseDeltaX;
            mouseRollDeltaY += mouseDeltaY * (double) inv;
            double dist = mouseRollDeltaX * mouseRollDeltaX + mouseRollDeltaY * mouseRollDeltaY;
            if (dist > 1.0D) {
                dist = MathHelper.sqrt_double(dist);
                double d = dist;
                if (dist > getMaxStickLength()) {
                    d = getMaxStickLength();
                }

                mouseRollDeltaX /= dist;
                mouseRollDeltaY /= dist;
                mouseRollDeltaX *= d;
                mouseRollDeltaY *= d;
            }
        }

    }

    public void onRenderTickPre(float partialTicks) {
        MCH_GuiTargetMarker.clearMarkEntityPos();
        if (!MCH_ServerSettings.enableDebugBoundingBox) {
            RenderManager.debugBoundingBox = false;
        }

        MCH_ClientEventHook.haveSearchLightAircraft.clear();
        if (super.mc != null && super.mc.theWorld != null) {
            for (Object currentItemstack : Minecraft.getMinecraft().theWorld.loadedEntityList) {
                if (currentItemstack instanceof MCH_EntityAircraft && ((MCH_EntityAircraft) currentItemstack).haveSearchLight()) {
                    MCH_ClientEventHook.haveSearchLightAircraft.add(currentItemstack);
                }
            }
        }

        if (!W_McClient.isGamePaused()) {
            EntityClientPlayerMP var17 = super.mc.thePlayer;
            if (var17 != null) {
                ItemStack var18 = var17.getCurrentEquippedItem();
                if (var18 != null && var18.getItem() instanceof MCH_ItemWrench && var17.getItemInUseCount() > 0) {
                    W_Reflection.setItemRendererProgress(1.0F);
                }

                ridingAircraft = MCH_EntityAircraft.getAircraft_RiddenOrControl(var17);
                if (ridingAircraft != null) {
                    cameraMode = ridingAircraft.getCameraMode(var17);
                } else if (var17.ridingEntity instanceof MCH_EntityGLTD) {
                    MCH_EntityGLTD ac = (MCH_EntityGLTD) var17.ridingEntity;
                    cameraMode = ac.camera.getMode(0);
                } else {
                    cameraMode = 0;
                }

                MCH_EntityAircraft var19 = null;
                if (!(var17.ridingEntity instanceof MCH_EntityHeli) && !(var17.ridingEntity instanceof MCP_EntityPlane) && !(var17.ridingEntity instanceof MCH_EntityTank)) {
                    if (var17.ridingEntity instanceof MCH_EntityUavStation) {
                        var19 = ((MCH_EntityUavStation) var17.ridingEntity).getControlAircract();
                    } else if (var17.ridingEntity instanceof MCH_EntityVehicle) {
                        MCH_EntityAircraft stickMode = (MCH_EntityAircraft) var17.ridingEntity;
                        stickMode.setupAllRiderRenderPosition(partialTicks, var17);
                    }
                } else {
                    var19 = (MCH_EntityAircraft) var17.ridingEntity;
                }

                boolean var20 = false;
                if (var19 instanceof MCH_EntityHeli) {
                    var20 = MCH_Config.MouseControlStickModeHeli.prmBool;
                }

                if (var19 instanceof MCP_EntityPlane) {
                    var20 = MCH_Config.MouseControlStickModePlane.prmBool;
                }

                for (int de = 0; de < 10 && prevTick > partialTicks; ++de) {
                    --prevTick;
                }

                float p;
                float r;
                if (var19 != null && var19.canMouseRot()) {
                    if (!isRideAircraft) {
                        var19.onInteractFirst(var17);
                    }

                    isRideAircraft = true;
                    this.updateMouseDelta(var20, partialTicks);
                    boolean var22 = false;
                    float var23 = 0.0F;
                    float var25 = 0.0F;
                    MCH_SeatInfo var26 = var19.getSeatInfo(var17);
                    if (var26 != null && var26.fixRot && var19.getIsGunnerMode(var17) && !var19.isGunnerLookMode(var17)) {
                        var22 = true;
                        var23 = var26.fixYaw;
                        var25 = var26.fixPitch;
                        mouseRollDeltaX *= 0.0D;
                        mouseRollDeltaY *= 0.0D;
                        mouseDeltaX *= 0.0D;
                        mouseDeltaY *= 0.0D;
                    } else if (var19.isPilot(var17)) {
                        MCH_AircraftInfo.CameraPosition var28 = var19.getCameraPosInfo();
                        if (var28 != null) {
                            var23 = var28.yaw;
                            var25 = var28.pitch;
                        }
                    }

                    if (var19.getAcInfo() == null) {
                        var17.setAngles((float) mouseDeltaX, (float) mouseDeltaY);
                    } else {
                        var19.setAngles(var17, var22, var23, var25, (float) (mouseDeltaX + prevMouseDeltaX) / 2.0F, (float) (mouseDeltaY + prevMouseDeltaY) / 2.0F, (float) mouseRollDeltaX, (float) mouseRollDeltaY, partialTicks - prevTick);
                    }

                    var19.setupAllRiderRenderPosition(partialTicks, var17);
                    double var29 = MathHelper.sqrt_double(mouseRollDeltaX * mouseRollDeltaX + mouseRollDeltaY * mouseRollDeltaY);
                    if (!var20 || var29 < getMaxStickLength() * 0.1D) {
                        mouseRollDeltaX *= 0.95D;
                        mouseRollDeltaY *= 0.95D;
                    }

                    p = MathHelper.wrapAngleTo180_float(var19.getRotRoll());
                    r = MathHelper.wrapAngleTo180_float(var19.getRotYaw() - var17.rotationYaw);
                    p *= MathHelper.cos((float) ((double) r * 3.141592653589793D / 180.0D));
                    if (var19.getTVMissile() != null && W_Lib.isClientPlayer(var19.getTVMissile().shootingEntity) && var19.getIsGunnerMode(var17)) {
                        p = 0.0F;
                    }

                    W_Reflection.setCameraRoll(p);
                    this.correctViewEntityDummy(var17);
                } else {
                    MCH_EntitySeat var21 = var17.ridingEntity instanceof MCH_EntitySeat ? (MCH_EntitySeat) var17.ridingEntity : null;
                    if (var21 != null && var21.getParent() != null) {
                        this.updateMouseDelta(var20, partialTicks);
                        var19 = var21.getParent();
                        boolean wi = false;
                        MCH_SeatInfo seatInfo = var19.getSeatInfo(var17);
                        if (seatInfo != null && seatInfo.fixRot && var19.getIsGunnerMode(var17) && !var19.isGunnerLookMode(var17)) {
                            wi = true;
                            mouseRollDeltaX *= 0.0D;
                            mouseRollDeltaY *= 0.0D;
                            mouseDeltaX *= 0.0D;
                            mouseDeltaY *= 0.0D;
                        }

                        Vec3 v = Vec3.createVectorHelper(mouseDeltaX, mouseRollDeltaY, 0.0D);
                        W_Vec3.rotateAroundZ((float) ((double) (var19.calcRotRoll(partialTicks) / 180.0F) * 3.141592653589793D), v);
                        MCH_WeaponSet ws = var19.getCurrentWeapon(var17);
                        mouseDeltaY *= ws != null && ws.getInfo() != null ? (double) ws.getInfo().cameraRotationSpeedPitch : 1.0D;
                        var17.setAngles((float) mouseDeltaX, (float) mouseDeltaY);
                        float y = var19.getRotYaw();
                        p = var19.getRotPitch();
                        r = var19.getRotRoll();
                        var19.setRotYaw(var19.calcRotYaw(partialTicks));
                        var19.setRotPitch(var19.calcRotPitch(partialTicks));
                        var19.setRotRoll(var19.calcRotRoll(partialTicks));
                        float revRoll = 0.0F;
                        if (wi) {
                            var17.rotationYaw = var19.getRotYaw() + seatInfo.fixYaw;
                            //System.out.println("yaw6");
                            var17.rotationPitch = var19.getRotPitch() + seatInfo.fixPitch;
                            if (var17.rotationPitch > 90.0F) {
                                var17.prevRotationPitch -= (var17.rotationPitch - 90.0F) * 2.0F;
                                var17.rotationPitch -= (var17.rotationPitch - 90.0F) * 2.0F;
                                var17.prevRotationYaw += 180.0F;
                                var17.rotationYaw += 180.0F;
                                //System.out.println("yaw7");
                                revRoll = 180.0F;
                            } else if (var17.rotationPitch < -90.0F) {
                                var17.prevRotationPitch -= (var17.rotationPitch - 90.0F) * 2.0F;
                                var17.rotationPitch -= (var17.rotationPitch - 90.0F) * 2.0F;
                                var17.prevRotationYaw += 180.0F;
                                var17.rotationYaw += 180.0F;
                                //System.out.println("yaw8");
                                revRoll = 180.0F;
                            }
                        }

                        var19.setupAllRiderRenderPosition(partialTicks, var17);
                        var19.setRotYaw(y);
                        //System.out.println("yaw9");
                        var19.setRotPitch(p);
                        var19.setRotRoll(r);
                        mouseRollDeltaX *= 0.9D;
                        mouseRollDeltaY *= 0.9D;
                        float roll = MathHelper.wrapAngleTo180_float(var19.getRotRoll());
                        float yaw = MathHelper.wrapAngleTo180_float(var19.getRotYaw() - var17.rotationYaw);
                        //System.out.println("yaw10");
                        roll *= MathHelper.cos((float) ((double) yaw * 3.141592653589793D / 180.0D));
                        //System.out.println("yaw11");
                        if (var19.getTVMissile() != null && W_Lib.isClientPlayer(var19.getTVMissile().shootingEntity) && var19.getIsGunnerMode(var17)) {
                            roll = 0.0F;
                        }

                        W_Reflection.setCameraRoll(roll + revRoll);
                        this.correctViewEntityDummy(var17);
                    } else {
                        if (isRideAircraft) {
                            W_Reflection.setCameraRoll(0.0F);
                            isRideAircraft = false;
                        }

                        mouseRollDeltaX = 0.0D;
                        mouseRollDeltaY = 0.0D;
                    }
                }

                if (var19 != null) {
                    if (var19.getSeatIdByEntity(var17) == 0 && !var19.isDestroyed()) {
                        var19.lastRiderYaw = var17.rotationYaw;
                        //System.out.println("yaw12");
                        var19.prevLastRiderYaw = var17.prevRotationYaw;
                        //System.out.println("yaw13");
                        var19.lastRiderPitch = var17.rotationPitch;
                        var19.prevLastRiderPitch = var17.prevRotationPitch;
                    }

                    var19.updateWeaponsRotation();
                }

                MCH_ViewEntityDummy var24 = MCH_ViewEntityDummy.getInstance(var17.worldObj);
                if (var24 != null) {
                    var24.rotationYaw = var17.rotationYaw;
                    //System.out.println("yaw14");
                    var24.prevRotationYaw = var17.prevRotationYaw;
                    //System.out.println("yaw15");
                    if (var19 != null) {
                        MCH_WeaponSet var27 = var19.getCurrentWeapon(var17);
                        if (var27 != null && var27.getInfo() != null && var27.getInfo().fixCameraPitch) {
                            var24.rotationPitch = var24.prevRotationPitch = 0.0F;
                        }
                    }
                }

                prevTick = partialTicks;
            }
        }
    }

    public void correctViewEntityDummy(Entity entity) {
        MCH_ViewEntityDummy de = MCH_ViewEntityDummy.getInstance(entity.worldObj);
        if (de != null) {
            if (de.rotationYaw - de.prevRotationYaw > 180.0F) {
                //System.out.println("yaw16");
                de.prevRotationYaw += 360.0F;
            } else if (de.rotationYaw - de.prevRotationYaw < -180.0F) {
                de.prevRotationYaw -= 360.0F;
                //System.out.println("yaw17");
            }
        }

    }

    public void onPlayerTickPre(EntityPlayer player) {
        if (player.worldObj.isRemote) {
            ItemStack currentItemstack = player.getCurrentEquippedItem();
            if (currentItemstack != null && currentItemstack.getItem() instanceof MCH_ItemWrench && player.getItemInUseCount() > 0 && player.getItemInUse() != currentItemstack) {
                int maxdm = currentItemstack.getMaxDamage();
                int dm = currentItemstack.getItemDamage();
                if (dm <= maxdm && dm > 0) {
                    player.setItemInUse(currentItemstack, player.getItemInUseCount());
                }
            }
        }

    }

    public void onPlayerTickPost(EntityPlayer player) {
    }

    @SubscribeEvent
    public void onRenderGameOverlayEvent(RenderGameOverlayEvent event) {
        float partialTicks = event.partialTicks;
        if (event.type == RenderGameOverlayEvent.ElementType.CROSSHAIRS && mc.thePlayer == null) {
            event.setCanceled(true);
            return;
        }

        Minecraft minecraft = Minecraft.getMinecraft();
        EntityPlayer player = minecraft.thePlayer;
        ScaledResolution scaledresolution = new ScaledResolution(minecraft, minecraft.displayWidth, minecraft.displayHeight);
        int i = scaledresolution.getScaledWidth();
        int j = scaledresolution.getScaledHeight();

        if (!event.isCancelable() && event.type == RenderGameOverlayEvent.ElementType.HELMET) {
            Minecraft.getMinecraft().entityRenderer.setupOverlayRendering();

            //渲染失明效果
            if (player != null && player.isPotionActive(Potion.blindness)
                && (player.ridingEntity instanceof MCH_EntityAircraft || player.ridingEntity instanceof MCH_EntitySeat || player.ridingEntity instanceof MCH_EntityUavStation)) {
                int amp = player.getActivePotionEffect(Potion.blindness).getAmplifier();
                int dur = player.getActivePotionEffect(Potion.blindness).getDuration();
                float alpha = 0.85f + Math.min(0.06f * (amp + 1), 0.17f);
                if (dur < 40) alpha *= (dur / 40.0f);
                GL11.glPushMatrix();
                GL11.glDisable(GL11.GL_DEPTH_TEST);
                GL11.glDepthMask(false);
                GL11.glEnable(GL11.GL_BLEND);
                GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
                GL11.glDisable(GL11.GL_TEXTURE_2D);
                Tessellator t = Tessellator.instance;
                t.startDrawingQuads();
                t.setColorRGBA(0, 0, 0, (int) (Math.max(0.0f, Math.min(0.98f, alpha)) * 255));
                t.addVertex(0, j, 0);
                t.addVertex(i, j, 0);
                t.addVertex(i, 0, 0);
                t.addVertex(0, 0, 0);
                t.draw();
                GL11.glEnable(GL11.GL_TEXTURE_2D);
                GL11.glDisable(GL11.GL_BLEND);
                GL11.glDepthMask(true);
                GL11.glEnable(GL11.GL_DEPTH_TEST);
                GL11.glPopMatrix();
            }

            if (this.mc.currentScreen == null || this.mc.currentScreen instanceof GuiChat || this.mc.currentScreen.getClass().toString().contains("GuiDriveableController")) {
                for (MCH_Gui gui : this.guis) {
                    if (drawGui(gui, partialTicks))
                        break;
                }
                drawGui(this.gui_Common, partialTicks);
                drawGui(this.gui_Wrench, partialTicks);
                drawGui(this.gui_EMarker, partialTicks);
                if (isDrawScoreboard)
                    MCH_GuiScoreboard.drawList(this.mc, this.mc.fontRenderer, false);
                drawGui(this.gui_Title, partialTicks);
            }

            //渲染第三人称准心
            if (player != null && showVehicleCrossHair) {
                final int scrW = scaledresolution.getScaledWidth();
                final int scrH = scaledresolution.getScaledHeight();
                final float cx = scrW * 0.5f;
                final float cy = scrH * 0.5f;
                final float sizePx = 32.0f;
                final float half = sizePx * 0.5f;
                GL11.glPushMatrix();
                GL11.glEnable(GL11.GL_BLEND);
                GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
                GL11.glDisable(GL11.GL_DEPTH_TEST);
                GL11.glDepthMask(false);

                GL11.glTranslatef(cx, cy, 0.0f);

                mc.getTextureManager().bindTexture(cross3rd);
                Tessellator t = Tessellator.instance;
                t.startDrawingQuads();
                t.addVertexWithUV(-half, half, 0, 0, 1);
                t.addVertexWithUV(half, half, 0, 1, 1);
                t.addVertexWithUV(half, -half, 0, 1, 0);
                t.addVertexWithUV(-half, -half, 0, 0, 0);
                t.draw();
                GL11.glDepthMask(true);
                GL11.glEnable(GL11.GL_DEPTH_TEST);
                GL11.glDisable(GL11.GL_BLEND);
                GL11.glPopMatrix();
            }
        }

        //渲染命中信息
        if (!event.isCancelable() && event.type == RenderGameOverlayEvent.ElementType.HOTBAR) {
            if (!hitList.isEmpty() && hitTotalDamage > 0) {
                int x = (int) (i * 0.6f);
                int y = (int) (j * 0.4f);
                GL11.glPushMatrix();
                float scale = hitTotalDamageScale;
                GL11.glScalef(scale, scale, scale);
                mc.fontRenderer.drawString(-(int) hitTotalDamage + "", (int) (x / scale), (int) (y / scale), 0xffffff, true);
                GL11.glPopMatrix();
            }
            int baseX = (int) (i * 0.6f);
            for (int idx = hitList.size() - 1, pos = 0; idx >= 0; idx--, pos++) {
                HitMessage message = hitList.get(idx);
                if (message.hitDisplay != null && message.hitDamage > 0) {
                    float yOffset = 0.45f + pos * 0.025f;
                    int y = (int) (j * yOffset);
                    float alpha = Math.max(0.0f, 1.0f - pos * 0.15f);
                    int a = ((int) (alpha * 255)) << 24;
                    int color = a | 0x00FFFFFF;
                    mc.fontRenderer.drawString(
                        String.format("%.1f %s", -message.hitDamage, message.hitDisplay),
                        baseX,
                        y,
                        color,
                        true
                    );
                }
            }
        }
    }

    public void onRenderTickPost(float partialTicks) {
        if (this.mc.thePlayer != null) {
            MCH_ClientTickHandlerBase.applyRotLimit(this.mc.thePlayer);
            MCH_ViewEntityDummy mCH_ViewEntityDummy = MCH_ViewEntityDummy.getInstance(this.mc.thePlayer.worldObj);
            if (mCH_ViewEntityDummy != null) {
                ((Entity) mCH_ViewEntityDummy).rotationPitch = this.mc.thePlayer.rotationPitch;
                ((Entity) mCH_ViewEntityDummy).rotationYaw = this.mc.thePlayer.rotationYaw;
                ((Entity) mCH_ViewEntityDummy).prevRotationPitch = this.mc.thePlayer.prevRotationPitch;
                ((Entity) mCH_ViewEntityDummy).prevRotationYaw = this.mc.thePlayer.prevRotationYaw;
            }
        }
//      if (this.mc.currentScreen == null || this.mc.currentScreen instanceof GuiChat || this.mc.currentScreen.getClass().toString().indexOf("GuiDriveableController") >= 0) {
//         for (MCH_Gui gui : this.guis) {
//            if (drawGui(gui, partialTicks))
//               break;
//         }
//         drawGui((MCH_Gui)this.gui_Common, partialTicks);
//         drawGui(this.gui_Wrench, partialTicks);
//         drawGui(this.gui_EMarker, partialTicks);
//         if (isDrawScoreboard)
//            MCH_GuiScoreboard.drawList(this.mc, this.mc.fontRenderer, false);
//         drawGui(this.gui_Title, partialTicks);
//      }
    }

    public boolean drawGui(MCH_Gui gui, float partialTicks) {
        if (gui.isDrawGui(super.mc.thePlayer)) {
            gui.drawScreen(0, 0, partialTicks);
            return true;
        } else {
            return false;
        }
    }

    public static class HitMessage {
        public String hitDisplay;
        public float hitDamage;
        public byte hitDamageType;
    }
}
